package com.ruoyi.wechat.api;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.degree.domain.*;
import com.ruoyi.degree.service.*;
import com.ruoyi.wechat.vo.IndexSchoolInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 学校信息API接口
 *
 * @author ruoyi
 */
@Api(tags = "学校信息管理")
@RestController
@RequestMapping("/wxapi/schools")
public class SchoolsInfoApi {

    @Autowired
    private IBizSchoolsService bizSchoolsService;
    @Autowired
    private IBizSchoolsProfessionsClassesService bizSchoolsProfessionsClassesService;
    @Autowired
    private IBizSchoolsProfessionsService bizSchoolsProfessionsService;
    @Autowired
    private IBizSchoolsVideosService bizSchoolsVideosService;
    @Autowired
    private IBizCommentsService bizCommentsService;
    @Autowired
    private IBizSchoolsDepartmentService bizSchoolsDepartmentService;
    @Autowired
    private IWechatUserStarsService wechatUserStarsService;

    @Autowired
    private IBizSchoolsMentorsService bizSchoolsMentorsService;

    /**
     * 获取学校列表（支持名称、培养计划、目的地查询条件）
     */
    @Anonymous
    @ApiOperation("获取学校列表")
    @GetMapping("/list")
    public AjaxResult list(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Long eduPlanId,
            @RequestParam(required = false) Long placeId,
            @RequestParam(required = false) Long isShow,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        int offset = (pageNum - 1) * pageSize;
        BizSchools query = new BizSchools();
        query.setName(name);
        query.setEduPlanId(eduPlanId);
        query.setPlaceId(placeId);
        query.setIsShow(isShow);
        startPage();
        List<BizSchools> schoolsList = bizSchoolsService.selectBizSchoolsList(query);

        List<IndexSchoolInfo> indexList = new ArrayList<>();
        for (BizSchools school : schoolsList) {
            IndexSchoolInfo info = new IndexSchoolInfo();
            info.setName(school.getName());
            info.setLogo(school.getLogo());
            info.setIntro(school.getIntro());
            info.setPlaceId(school.getPlaceId());
            info.setEduPlanId(school.getEduPlanId());
            info.setOpeningDateId(school.getOpeningDateId());
            info.setId(school.getId());
            info.setCityName(school.getCityName());

            // 对 openingDates 去重
            String openingDates = school.getOpeningDates();
            if (openingDates != null && !openingDates.isEmpty()) {
                Set<String> set = new LinkedHashSet<>(Arrays.asList(openingDates.split(",")));
                openingDates = String.join(",", set);
            }
            info.setOpeningDates(openingDates);

            indexList.add(info);
        }
        return AjaxResult.success(indexList);
    }

    /**
     * 根据学校ID获取详细信息
     */
    @Anonymous
    @ApiOperation("根据学校ID获取详细信息")
    @GetMapping("/getSchoolInfo")
    public AjaxResult getSchoolInfo(@RequestParam Long schoolId) {
        return AjaxResult.success(bizSchoolsService.selectBizSchoolsById(schoolId));
    }

    /**
     * getSchoolsIndex
     * 获取大学详情页中所有数据
     */
    @Anonymous
    @ApiOperation("根据学校ID获取详细信息")
    @GetMapping("/getSchoolsIndex")
    public AjaxResult getSchoolsIndex(@RequestParam Long schoolId, @RequestParam Long userId) {
        //获取大学详情
        BizSchools schools = bizSchoolsService.selectBizSchoolsById(schoolId);
        if (schools == null) {
            return AjaxResult.error("学校信息不存在");
        }
        //获取学院列表
        BizSchoolsDepartment bizSchoolsDepartment = new BizSchoolsDepartment();
        bizSchoolsDepartment.setSchoolId(schoolId);
        List<BizSchoolsDepartment> deptList = bizSchoolsDepartmentService.selectBizSchoolsDepartmentList(bizSchoolsDepartment);
        for (BizSchoolsDepartment department : deptList) {
            // 获取该学院的专业列表
            BizSchoolsProfessions bizSchoolsProfessions = new BizSchoolsProfessions();
            bizSchoolsProfessions.setSchoolId(schoolId);
            bizSchoolsProfessions.setDepartmentId(department.getId());
            List<BizSchoolsProfessions> professionsList = bizSchoolsProfessionsService.selectBizSchoolsProfessionsList(bizSchoolsProfessions);
            department.setProfessions(professionsList);
        }
        if(userId!= null && userId >0){
            //判断是否收藏
            WechatUserStars wechatUserStars = new WechatUserStars();
            wechatUserStars.setWechatUserId(userId);
            wechatUserStars.setObjectId(schoolId);
            List<WechatUserStars> starsList = wechatUserStarsService.selectWechatUserStarsList(wechatUserStars);
            if (starsList != null && starsList.size() > 0) {
                schools.setIsStar(1);
            } else {
                schools.setIsStar(0);
            }
        }

        //获取该大学的视频信息
        BizSchoolsVideos bizSchoolsVideos = new BizSchoolsVideos();
        bizSchoolsVideos.setSchoolId(schoolId);
        List<BizSchoolsVideos> videos = bizSchoolsVideosService.selectBizSchoolsVideosList(bizSchoolsVideos);
        //获取顾问留言
        BizSchoolsMentors bizSchoolsMentors = new BizSchoolsMentors();
        bizSchoolsMentors.setSchoolId(schoolId);
        List<BizSchoolsMentors> mentors = bizSchoolsMentorsService.selectBizSchoolsMentorsList(bizSchoolsMentors);
        return AjaxResult.success().put("school", schools)
                .put("departments", deptList)
                .put("videos", videos)
                .put("mentors", mentors);
    }

    /**
     * 根据schoolid获取院校详情
     */

    @Anonymous
    @ApiOperation("根据学校ID获取院校详情")
    @GetMapping("/{schoolId}/detail")
    public AjaxResult getSchoolDetail(@ApiParam(value = "学校ID", required = true) @PathVariable Long schoolId) {
        BizSchools school = bizSchoolsService.selectBizSchoolsById(schoolId);
        if (school == null) {
            return AjaxResult.error("学校信息不存在");
        }
        BizSchoolsProfessions bizSchoolsProfessions = new BizSchoolsProfessions();
        bizSchoolsProfessions.setSchoolId(school.getId());
        List<BizSchoolsProfessions> list = bizSchoolsProfessionsService.selectBizSchoolsProfessionsList(bizSchoolsProfessions);

        BizSchoolsVideos bizSchoolsVideos = new BizSchoolsVideos();
        bizSchoolsVideos.setSchoolId(school.getId());
        List<BizSchoolsVideos> videos = bizSchoolsVideosService.selectBizSchoolsVideosList(bizSchoolsVideos);

        IndexSchoolInfo info = new IndexSchoolInfo();
        info.setProfessions(list);
        // 这里可以继续设置你需要的其他字段
        info.setVideos(videos);
        return AjaxResult.success(info);
    }

    @Anonymous
    @ApiOperation("根据学校ID获取课程信息")
    @GetMapping("/searchProfessions")
    public AjaxResult searchProfessions(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String plan,
            @RequestParam(required = false) String destination,
            @RequestParam(required = false) String startTime,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        params.put("plan", plan);
        params.put("destination", destination);
        params.put("startTime", startTime);
        int offset = (pageNum - 1) * pageSize;
        params.put("offset", offset);
        params.put("pageSize", pageSize);

        List<BizSchoolsProfessions> list = bizSchoolsProfessionsService.selectProfessionsBySearch(params);
        return AjaxResult.success(list);
    }
    @Anonymous
    @ApiOperation("根据学校地区，开学时间，培养计划获取详细信息")
    @GetMapping("/getSchoolsList")
    public AjaxResult getSchoolsList(
            @RequestParam(required = false) Long placeId,
            @RequestParam(required = false) Long eduPlanId,
            @RequestParam(required = false) String openingDateId,
            @RequestParam(required = false) String openingDates,
            @RequestParam(required = false) Long isShow,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        List<String> openingDateIdList = null;
        if (openingDateId != null && !openingDateId.trim().isEmpty()) {
            openingDateIdList = Arrays.asList(openingDateId.split(","));
        }
        int offset = (pageNum - 1) * pageSize;
        BizSchools query = new BizSchools();
        query.setPlaceId(placeId);
        query.setEduPlanId(eduPlanId);
        query.setOpeningDates(openingDates);
        query.setIsShow(isShow);
        startPage();
        List<BizSchools> schoolsList = bizSchoolsService.selectBizSchoolsList(query);

        for (BizSchools school : schoolsList) {
            String dates = school.getOpeningDates();
            if (dates != null && !dates.isEmpty()) {
                Set<String> set = new LinkedHashSet<>(Arrays.asList(dates.split(",")));
                school.setOpeningDates(String.join(",", set));
            }
        }
        return AjaxResult.success(schoolsList);
    }

    @Anonymous
    @ApiOperation("根据地点获取培养计划信息")
    @GetMapping("/getEduPlanIdByPlaceId/{placeId}")
    public AjaxResult getEduPlanIdByPlaceId(@PathVariable Long placeId) {
        List<Long> eduplanIdList = bizSchoolsService.selectEduPlanIdByPlaceId(placeId);
        return AjaxResult.success(eduplanIdList);
    }

    //getDepartmentBySchoolId
    @Anonymous
    @ApiOperation("根据学校ID获取院系列表")
    @GetMapping("/getDepartmentBySchoolId")
    public AjaxResult getDepartmentBySchoolId(@RequestParam Long schoolId,@RequestParam String teachLanguage, @RequestParam String openingDate) {
//获取学院列表
        BizSchoolsDepartment bizSchoolsDepartment = new BizSchoolsDepartment();
        bizSchoolsDepartment.setSchoolId(schoolId);
        List<BizSchoolsDepartment> deptList = bizSchoolsDepartmentService.selectBizSchoolsDepartmentList(bizSchoolsDepartment);
        for (BizSchoolsDepartment department : deptList) {
            // 获取该学院的专业列表
            BizSchoolsProfessions bizSchoolsProfessions = new BizSchoolsProfessions();
            bizSchoolsProfessions.setSchoolId(schoolId);
            bizSchoolsProfessions.setDepartmentId(department.getId());
            bizSchoolsProfessions.setLanguages(teachLanguage);
            bizSchoolsProfessions.setOpeningDates(openingDate);
            List<BizSchoolsProfessions> professionsList = bizSchoolsProfessionsService.selectBizSchoolsProfessionsList(bizSchoolsProfessions);
            department.setProfessions(professionsList);
        }
        return AjaxResult.success(deptList);
    }

}